/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Processing;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 *
 */
public class ChessController extends GameController
{
    private static byte[] InitialPawnList = { ChessPawn.XeDen, ChessPawn.MaDen, ChessPawn.TuongDen, ChessPawn.HauDen, ChessPawn.VuaDen, ChessPawn.TuongDen, ChessPawn.MaDen, ChessPawn.XeDen };

    public void InitController(int qcDiTruoc)
    {
        board = new ChessBoard();
        m_ChessType = 0;
        m_BoardWidth = 240;
        m_BoardHeight = 240;
        m_SquareSize = 30;
        m_SquareNumber = 8;
        m_NextImageIndex = 13;
        m_SelectedImageIndex = 14;
        m_CollumnSquareNum = 7;
        m_RowSquareNum = 7;
        
        board.InitBoard(qcDiTruoc);
        PawnList.clear();
        board.PawnList(PawnList);
        MovablePawn = qcDiTruoc;
        m_Instance = false;
    }

    public void LoadState(byte[] dl)
    {
        board = new ChessBoard();
        m_ChessType = 0;
         m_BoardWidth = 240;
        m_BoardHeight = 240;
        m_SquareSize = 30;
        m_SquareNumber = 8;
        m_NextImageIndex = 13;
        m_SelectedImageIndex = 14;
        m_CollumnSquareNum = 7;
        m_RowSquareNum = 7;

        board.LoadBoard(dl);
        PawnList.clear();
        board.PawnList(PawnList);
        MovablePawn = (int)dl[0];
        m_Instance = false;
    }

    public Hashtable[] PlaceAMove(int vtDau, int vtCuoi, int loaiPhong)
    {
        Hashtable[] result = new Hashtable[3];
        result[0] = new Hashtable();
        result[1] = new Hashtable();
        result[2] = new Hashtable();
        byte temp1 = ChessPawn.OTrong;
        byte temp2 = ChessPawn.OTrong;

        if (!PawnList.containsKey(new Integer(vtDau)))
            return null;

        temp1 = ((Integer)PawnList.get(new Integer(vtDau))).byteValue();
        PawnList.remove(new Integer(vtDau));
        if (PawnList.containsKey(new Integer(vtCuoi)))
        {
            temp2 = ((Integer)PawnList.get(new Integer(vtCuoi))).byteValue();
            PawnList.remove(new Integer(vtCuoi));
        }

        Enumeration en = PawnList.keys();
        while (en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            result[0].put(each, PawnList.get(each));
        }
        result[1].put(new Integer(vtDau), new Integer(temp1));
        result[1].put(new Integer(vtCuoi), new Integer(temp2));
        int[] moveResult = board.PlaceAMove(vtDau, vtCuoi, loaiPhong);
        if (moveResult != null)
        {
            if (moveResult[0] != -1)
            {
                result[2].put(new Integer(vtCuoi), new Integer(moveResult[1]));
                PawnList.put(new Integer(vtCuoi), new Integer(moveResult[1]));
            }
            else
            {
                result[2].put(new Integer(vtCuoi), new Integer(temp1));
                PawnList.put(new Integer(vtCuoi), new Integer(temp1));
            }
            MovablePawn = (MovablePawn == 0) ? 1 : 0;
        }
        else
        {
            PawnList.put(new Integer(vtDau), new Integer(temp1));
            if(temp2 != ChessPawn.OTrong)
                PawnList.put(new Integer(vtCuoi), new Integer(temp2));
            return null;
        }

        return result;
    }

    public Hashtable[] PlaceAMove(int vtDau, int vtCuoi)
    {
        return PlaceAMove(vtDau, vtCuoi, 1);
    }
}
